// pages/order/index.js
import {request} from '../../request/index.js'
Page({
    data: {
        tabs:[
            {
                id:0,
                value:'综合',
                isActive:true
            },
            {
                id:1,
                value:'待付款',
                isActive:false
            },
            {
                id:2,
                value:'待发货',
                isActive:false
            },
            {
                id:3,
                value:'退款/退货',
                isActive:false
            },
        ],
        orders:[
            {order_id:1,order_number:'012412412421412421',order_price:666,order_time:'2022-01-02'},
            {order_id:2,order_number:'022412412421412421',order_price:777,order_time:'2022-01-02'},
            {order_id:3,order_number:'032412412421412421',order_price:888,order_time:'2022-01-02'},
            {order_id:4,order_number:'042412412421412421',order_price:999,order_time:'2022-01-02'},
        ]
    },
    // 根据标题索引来激活选中标题数组
    changeTitleByIndex(index){
        // 2 修改源数组
        let {tabs}=this.data;
        tabs.forEach((v,i) => {
            i===index?v.isActive=true:v.isActive=false
        });
        // 3 赋值到data中
        this.setData({
            tabs
        })
    },
    // 标题点击事件
    handleTabsItemChange(e){
        // 1 获取被点击的标题索引
        const {index}=e.detail
        changeTitleByIndex(index)
        
    },
    // 获取订单列表的方法
    async getOrders(type){
        const res=await request({url:'/my/orders/all',data:{type}})
        console.log(res);
    },
    onShow(options) {
        const token=wx.getStorageSync('token');
        if(!token){
            wx.navigateTo({
                url: '/pages/auth/index',
            });
            return
        }
          
        let pages= getCurrentPages();
        // 数组中 索引最大的页面就是当前页面
        let currentPage=pages[pages.length-1]
        // 获取url上的type参数
        const {type}=currentPage.options
        // 激活选中页面标题，当type=1，index=0
        this.changeTitleByIndex(type-1)
        console.log(type);
        // this.getOrders(type)
        
          
    },

    
})